package com.lenovo.agricultureServer.util;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.NameValuePair;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;

public class Util
{
	public static double getTwoPointDistance(double lat1, double lon1, double lat2, double lon2) 
	{     
        float[] results=new float[1];     
        Location.distanceBetween(lat1, lon1, lat2, lon2, results);     
        return results[0];
    } 
	
	public static String nameValuePairtToString(ArrayList<NameValuePair> dataList)
	{
		String resultStr = "";
		if(dataList!=null)
		{
			for(int i=0; i<dataList.size(); i++)
			{
				NameValuePair dataPair = dataList.get(i);
				if(i>0)
				{
					resultStr += "&";
				}
				resultStr += dataPair.getName()+"="+dataPair.getValue();
			}
		}
		return resultStr;
	}
	
	public static void showCommonDialog(Activity activity, String title, String message)
	{
		AlertDialog.Builder customDia=new AlertDialog.Builder(activity);
		customDia.setTitle(title);
		customDia.setMessage(message);
        customDia.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which) 
            {
            }
        });
        customDia.create().show();
	}
	
	public static void intentSendEmail(Activity activity, String mailAddr)
	{
		if(activity!=null && !mailAddr.equals(""))
		{
			Intent it = new Intent(Intent.ACTION_SEND); 
			String[] receiver;
			receiver=new String[]{mailAddr};
			it.putExtra(Intent.EXTRA_EMAIL, receiver);
			it.setType("text/plain"); 
			activity.startActivity(Intent.createChooser(it, "Choose Email Client"));
		}
	}

    public static Date getDateByTimeMillis(String sendDate) {
        try {
            long time = Long.parseLong(sendDate);
            return  new Date(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

	public static Date getDateByString(String dateStr) 
	{     
		Date date = null; 
        if(dateStr!=null && !dateStr.equals(""))
        {
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        	try {
				date = sdf.parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
        }
        return date;
    } 
	
	public static Date getDateAndTimeByString(String dateTimeStr) 
	{     
		Date date = null; 
        if(dateTimeStr!=null && !dateTimeStr.equals(""))
        {
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	try {
				date = sdf.parse(dateTimeStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
        }
        return date;
    }
	
	//判断当前系统中指定的service是否在运行
	public static boolean isServiceExisted(Context context, String className) 
	{
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);

        if(serviceList.size()<= 0) {
            return false;
        }

        for(int i = 0; i < serviceList.size(); i++) {
            RunningServiceInfo serviceInfo = serviceList.get(i);
            ComponentName serviceName = serviceInfo.service;

            if(serviceName.getClassName().equals(className)) {
                return true;
            }
        }
        return false;
    }
	
	private static String getTopPkgName(Context context)
    {
    	ActivityManager manager = (ActivityManager)context.getSystemService (Context.ACTIVITY_SERVICE);
    	List<RunningTaskInfo>taskList = manager.getRunningTasks(2);
    	if(taskList.size()>0 && taskList.get(0)!=null)
    	{
    		RunningTaskInfo secActivity =taskList.get(1);
        	return secActivity.baseActivity.getPackageName().toString();
    	}
    	return null;
    }
	
	public static String getNumberForString(String oldStr)
    {
		if(oldStr != null)
		{
			String regEx="[^0-9]";   
			Pattern p = Pattern.compile(regEx);   
			Matcher m = p.matcher(oldStr);
			return m.replaceAll("").trim();
		}
    	return null;
    }
	
	public static void RecursionDeleteFile(File file){
        if(file.isFile()){
            file.delete();
            return;
        }
        if(file.isDirectory()){
            File[] childFile = file.listFiles();
            if(childFile == null || childFile.length == 0){
                file.delete();
                return;
            }
            for(File f : childFile){
                RecursionDeleteFile(f);
            }
            file.delete();
        }
    }

}
